<?php
require("../dbconnect.php");
require("../email.php");
?>

<table width="100%" cellpadding="5" cellspacing="0" align="left"  bgcolor="#ffffff">

    <tr>
        <td  width="287" height="100" align="center">
            <br /><br />
            <?
            if (!empty($_GET['item'])) {
                $id = $_GET['item'];
                $pid = $_GET['pid'];
                $quantity = $_GET['quantity'];




                $chk = mysql_query("select * from os_product_inventory where id=$id") or die(mysql_error());

                if (!ctype_digit($quantity) || $quantity == 0) {
                    ?>
                    <font class="dw_failed">Please enter valid quantity value!</font>
                    <br /><br />
            <center><input type="button" class="button" value="Close" onClick="closeColorBox()"></center>

            <?
        } elseif (mysql_result($chk, 0, "quantity") < $quantity) {
            ?>
            <font class="dw_failed">Sorry, we are unable to cater to the amount of quantity requested. </font>
            <br /><br />
            <center><input type="button" class="button" value="Close" onClick="closeColorBox()"></center>

            <?
        } else {
            $error = 0;
            if (!empty($_SESSION['cart'])) {
                foreach ($_SESSION['cart'] as $cartItems) {
                    if ($cartItems['id'] == $id) {
                        $error = 1;
                        break;
                    }
                }
            } else {
                $error = 0;
            }


            if ($error == 1) {
                ?>
                <font class="dw_failed">This product is already in your cart</font>
                <br /><br />
                <center><input type="button" value="View My Cart" class="button" onclick="window.open('os_mycart.php','_self')" />
                    <input type="button" class="button" value="Close" onClick="closeColorBox()">
                </center>

                <?
            } else {
                if (!empty($_GET['addon_id'])) {
                    $addon_ids = $_GET['addon_id'];
                    $total_addon = count($addon_ids);
                } else {
                    $addon_ids = 0;
                    $total_addon = 0;
                }

                $_SESSION['cart'][] = array(
                    "quantity" => $quantity,
                    "id" => $id,
                    "addon_id" => $addon_ids,
                    ""
                );

                $total_item = 0;
                $total_piece = 0;
                $total_allprice = 0;
                $no = 0;

                foreach ($_SESSION['cart'] as $cartItems) {
                    $itemId = $cartItems['id'];
                    $quantity = $cartItems['quantity'];
                    $addon_ids = $cartItems['addon_id'];


                    $get_items = mysql_query("select * from os_product_inventory where id=" . $itemId);
                    $item = mysql_fetch_array($get_items, MYSQL_ASSOC);
                    $proId = $item['pid'];
                    $get_product = mysql_query("select * from os_products where id=$proId") or die(mysql_error());


                    $a = mysql_fetch_array($get_product, MYSQL_ASSOC);

                    $total_price = 0;


                    $total_price = $item['unit_price'] * $quantity;
                    $unit_price = $item['unit_price'];

                    $total_allprice = $total_allprice + $total_price;

                    $total_item++;
                    $total_piece = $total_piece + $quantity;
                    $no++;
                    if (is_array($addon_ids)) {
                        for ($addon = 0; $addon < count($addon_ids); $addon++) {
                            $addon_id = $addon_ids[$addon];
                            $get_addon = mysql_query("select * from os_products where id='" . $addon_id . "'");
                            $ap = mysql_fetch_array($get_addon, MYSQL_ASSOC);

                            $get_addon_items = mysql_query("select * from os_product_inventory where pid='" . $addon_id . "' order by id asc limit 1") or die(mysql_error());

                            $addon_item = mysql_fetch_array($get_addon_items, MYSQL_ASSOC);


                            $unit_price = $addon_item['unit_price'];
                            $total_price = $quantity * $unit_price;

                            $total_allprice = $total_allprice + $total_price;
                        }
                    }
                }

                $_SESSION['total_items'] = $total_piece;
                $_SESSION['total_overall_price'] = $total_allprice;

                $get_items = mysql_query("select * from os_product_inventory where id=" . $id);
                $item = mysql_fetch_array($get_items, MYSQL_ASSOC);

                $get_product = mysql_query("select * from os_products where id=$pid");

                $a = mysql_fetch_array($get_product, MYSQL_ASSOC);


                $get_pic = mysql_query("select * from os_productpic where product_id=" . $a['id'] . " order by order_id asc limit 1");

                if (mysql_num_rows($get_pic) == 0) {
                    $pic = "empty";
                    $piclink = "noimage.jpg";
                    $fix = "";
                } else {
                    $pic = mysql_fetch_array($get_pic, MYSQL_ASSOC);

                    $piclink = $pic['name'];

                    if ($pic["width"] - $pic["height"] > 0) {
                        $fix = "width=80";
                    } else {
                        $fix = "height=80";
                    }
                }
                ?>
                <font class="dw_success"><?= $quantity ?> item(s) added into shopping cart!</font><br />
                <img style="border:1px solid #FFF" src="productpic/thumbnails/<?= $piclink ?>" <?= $fix ?>>
                <br />
                <font color="#000000"><?= $a['product_name_eng'] ?> (<?= $item['description'] ?>)</font>
                <?
                if ($total_addon > 0) {
                    ?>
                    with <?= $total_addon ?> add-on.
                    <?
                }
                ?>
                <br /><br />
                <center>
                    <input type="button" value="View My Cart" class="button" onclick="window.open('os_mycart.php','_self')" />
                    <input type="button" class="button" value="Close" onClick="closeColorBox()">
                </center>


                <?
            }
        }
        ?>
        <?
    } else {
        ?>
        <font class="dw_failed">Illegal Operation!</font>
        <?
    }
    ?>


</td>
</tr>
</table> 	